package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.Industry;
import cn.anxingongyou.anxin.service.IndustryService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class IndustryController {

    private final IndustryService industryService;
    @Autowired
    public IndustryController(IndustryService industryService) {
        this.industryService = industryService;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加行业信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/industry/add",method = RequestMethod.POST)
    public Map addIndustry(
            @RequestBody @ApiParam(value = "行业信息", required = true) Industry industry
            ){
        String key = "industryList";
        if(redisUtil.hasKey(key)) {
            // 已缓存 删除缓存
            redisUtil.del(key);
        }
        industry.setCreated(new Date());
        industry.setUpdated(new Date());
        int res = industryService.insert(industry);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除行业信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/industry/del/{id}",method = RequestMethod.GET)
    public Map delIndustry(
            @PathVariable @ApiParam(value = "行业主键", required = true) Long id
    ){
        String key = "industryList";
        if(redisUtil.hasKey(key)) {
            // 已缓存 删除缓存
            redisUtil.del(key);
        }
        int res = industryService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新行业信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/industry/update",method = RequestMethod.POST)
    public Map updateIndustry(
            @RequestBody @ApiParam(value = "行业信息,必传主键", required = true) Industry industry
    ){
        String key = "industryList";
        if(redisUtil.hasKey(key)) {
            // 已缓存 删除缓存
            redisUtil.del(key);
        }
        int res = industryService.updateByPrimaryKeySelective(industry);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取行业信息通过实体")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/industry/findByIndustry",method = RequestMethod.POST)
    public Map findOneIndustry(
            @RequestBody @ApiParam(value = "行业信息", required = true) Industry industry
    ){
        Industry industry1 = industryService.selectOne(industry);
        if(industry1 != null) {
            return Result.<Industry>instance(200,"获取成功",industry1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取行业信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/industry/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "管理员主键", required = true) Long id
    ){
        Industry industry = industryService.selectOneByPrimaryKey(id);
        if(industry != null) {
            return Result.<Industry>instance(200,"获取成功",industry).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取行业信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/industry/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByIndustry(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "行业信息", required = true)  Industry industry){
        PageInfo<Industry> pageInfo = industryService.page(pageNum, pageSize, industry);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取行业信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/industry/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectByIndustry(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "行业信息", required = true)  Industry industry){
        String key = "industryList";
        if(redisUtil.hasKey(key)){
            // 已缓存
            Map<String,Object> res = (Map<String,Object>) redisUtil.get(key);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        }else {
            // 未被缓存
            PageInfo<Industry> pageInfo = industryService.page(pageNum, pageSize, industry);
            Map<String,Object> res = new HashMap<>();
            res.put("total", pageInfo.getTotal());
            res.put("pageNum", pageInfo.getPageNum());
            res.put("pageSize", pageInfo.getPageSize());
            res.put("list",pageInfo.getList());
            redisUtil.set(key,res);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        }

    }
}
